package com.wbvideo.resample;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    String[] allpermissions = new String[]{Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.CAMERA,
            Manifest.permission.RECORD_AUDIO,
            Manifest.permission.CALL_PHONE};
    private long nativeReSampler = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        applypermission();
    }

    public void applypermission() {
        if (Build.VERSION.SDK_INT >= 23) {
            boolean needapply = false;
            for (int i = 0; i < allpermissions.length; i++) {
                int chechpermission = ContextCompat.checkSelfPermission(getApplicationContext(),
                        allpermissions[i]);
                if (chechpermission != PackageManager.PERMISSION_GRANTED) {
                    needapply = true;
                }
            }
            if (needapply) {
                ActivityCompat.requestPermissions(MainActivity.this, allpermissions, 1);
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    public void initReSampler(int inChannels, int inSampleRate, int outChannels,
                              int outSampleRate) {
        nativeReSampler = AudioReSampleUtils.initReSample(inChannels, inSampleRate,
                outChannels, outSampleRate);
    }

    public void endResample() {
        if (nativeReSampler == 0)
            return;
        AudioReSampleUtils.endResample(nativeReSampler);
    }

    public byte[] pushAudioData(byte[] buffer, int bytelen) {
        if (nativeReSampler == 0)
            return buffer;
        return AudioReSampleUtils.reSampleBuffer(nativeReSampler, buffer, bytelen);
    }


    public void sampleWavFile(View view) {
        initReSampler(2, 44100, 2, 48000);


        File mFile = new File("/sdcard/music_44100.wav");
        try {
            if (!mFile.exists()) {
                return;
            }
            FileInputStream ins = new FileInputStream(mFile);
            File outFile = new File("/sdcard/music_48000_resample.wav");
            FileOutputStream out = new FileOutputStream(outFile);

            byte[] buf = new byte[1024 * 2 * 2];
            boolean isFinish = false;
            float len = 0;
            while ((len = ins.read(buf)) != -1) {
                long time = System.currentTimeMillis();

                byte[] nsProcessData = pushAudioData(buf, buf.length);
                out.write(nsProcessData);
                isFinish = true;
            }

            if (isFinish)
                Toast.makeText(getApplicationContext(), "完成", Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            e.printStackTrace();
        }

        endResample();
    }

    public void samplePlayMusic(View view) {
        try {
            MusicDataManager.getInstance().startPlayMusic("/sdcard/music.mp3", 48000, 0.5f);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void sampleStopMusic(View view) {
        MusicDataManager.getInstance().stopPlayMusic();
    }
}
